<?php
namespace app\house\controller;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\house\model\AdminCate as cate_model;
use app\house\model\AdminHouse;
class AdminCate extends AdminRest
{

    protected $model;
    //房源模型
    protected $house_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model       = new cate_model();

        $this->house_model = new AdminHouse();
    }

    /**
     * 房产分类列表
     */
    public function cateList(){

        $input= $this->_param;
        if(!empty($input['keyword'])){
            $dis[] = ['title','like',"%".$input['keyword']."%"];
        }
        if(!empty($input['status'])){
            $dis[] = ['status','=',$input['status']];
        }
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data = $this->model->cateList($dis,$input['limit']);
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-16 16:45
     * @功能说明:根据格式获取分类
     */
    public function getCateData(){
        //分类类型
        $data = ['售房','简洁','楼盘'];

        $new_data = [];

        foreach ($data as $k=>$v){
            //类型
            $new_data[$k]['value'] = $k;
            //名称
            $new_data[$k]['label'] = $v;
            //分类数据
            $new_data[$k]['children'] = Db::name('longbing_card_house_category')->where(['uniacid'=>$this->_uniacid,'type'=>$k,'status'=>1])->field('id as value,title as label')->select();
        }
        return $this->success($new_data);
    }

    /**
     *
     * 添加房产分类
     */
    public function cateAdd(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $input['img']     = implode(',',$input['img']);
        $res = $this->model->cateAdd($input);
        return $this->success($res);
    }

    /**
     *
     * 编辑房产分类
     */
    public function cateUpdate(){
        $input = $this->_input;
        if(count($input)>1){
            if(!empty($input['img'])){
                $input['img'] = implode(',',$input['img']);
            }
            $data = $this->model->cateUpdate(['id'=>$input['id']],$input);
        }else{
            $data = $this->model->cateInfo(['id'=>$input['id']]);
            if(!empty($data) && !empty('img')) $data = transImagesOne($data ,['img'] ,$this->_uniacid);
        }
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-01-09 10:16
     * @功能说明:删除房源分类
     */
    public function cateDel(){
        $input = $this->_input;
        //房源查询条件
        $house_dis = [
           'uniacid'  => $this->_uniacid,
           'category' => $input['id']
        ];
        //查询房源
        $house = $this->house_model->houseInfo($house_dis);
        if(!empty($house)){
            $this->errorMsg('该分类正在被使用，无法删除');
        }
        //删除分类
        $res = $this->model->cateUpdate(['id'=>$input['id']],['status'=>-1]);
        return $this->success($res);
    }




}
